/**
 * 
 */
package com.oolong.account.web;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.oolong.account.model.UserModel;
import com.oolong.account.service.ExtraService;
import com.oolong.account.util.ParamterCheck;

/**
 * @author beralee
 * 
 */
@Controller
@RequestMapping("/createuser.do")
public class CreateUserController {
	@Autowired
	private ExtraService extraService;
	String message = " ";

	@RequestMapping(method = RequestMethod.GET)
	public String process(ModelMap model, HttpServletResponse response,
			HttpSession session) {
		response.setContentType("text/html;charset=UTF-8");
		message = " ";
		model.addAttribute("message", message);
		return "create";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String process(@RequestParam("username") String username,
			@RequestParam("password") String password,
			@RequestParam("email") String email, ModelMap model,
			HttpServletResponse response, HttpSession session) {
		response.setContentType("text/html;charset=UTF-8");
		try {
			if (extraService.validateUserExist(username)) {
				message = "用户名已存在";
				model.addAttribute("message", message);
				return "deleteOutItem";
			}
			// 校验用户名
			if (!ParamterCheck.checkName(username)) {
				message = "用户名只能为字母（大小写）、数字、中文、下划线的组合，最大不得超过15位";
				model.addAttribute("message", message);
				return "deleteOutItem";
			}
			// 校验密码
			if (!ParamterCheck.checkPassword(password)) {
				message = "用户的密码只能为字母（大小写）或数字的组合，最大不得超过15位";
				model.addAttribute("message", message);
				return "deleteOutItem";
			}
			// 校验邮箱
			if (!ParamterCheck.checkMemo(email)) {
				message = "输入的所有邮箱总长度最大不得超过128位，请减少邮箱个数";
				model.addAttribute("message", message);
				return "deleteOutItem";
			}
			String[] emails = email.split(";");
			for (int i = 0; i < emails.length; i++) {
				if (!ParamterCheck.checkEmail(emails[i])) {
					message = "输入的邮箱格式不正确，格式错误的邮箱为：" + emails[i] + "，请返回修改";
					model.addAttribute("message", message);
					return "deleteOutItem";
				}
			}
			int user_id = extraService.createUser(username, password, email);
			UserModel userModel = new UserModel();
			userModel.setId(user_id);
			userModel.setName(username);
			userModel.setEmail(email);
			session.setAttribute("user", userModel);
		} catch (DuplicateKeyException e) {
			message = "用户ID已存在";
			model.addAttribute("message", message);
			return "create";
		} catch (Exception e) {
			message = "创建用户失败";
			model.addAttribute("message", message);
			return "deleteOutItem";
		}
		message = "创建用户成功,您可以返回登录页面进行登录";
		model.addAttribute("message", message);
		return "create";
	}
}
